/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.jackson.codec;

import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperBuilder;
import io.micronaut.context.BeanProvider;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Secondary;
import org.jspecify.annotations.Nullable;
import io.micronaut.http.MediaType;
import io.micronaut.http.codec.CodecConfiguration;
import io.micronaut.jackson.databind.JacksonDatabindMapper;
import io.micronaut.runtime.ApplicationConfiguration;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

/**
 * A {@link io.micronaut.http.codec.MediaTypeCodec} for JSON and Jackson.
 * <p>
 * Note: will be replaced by {@link io.micronaut.json.codec.JsonMediaTypeCodec} in the future, but that class is
 * currently experimental.
 *
 * @author Graeme Rocher
 * @since 1.0.0
 * @deprecated Replaced with message body writers / readers API
 */
@Named("json")
@Singleton
@Secondary
@BootstrapContextCompatible
@Bean(typed = {JsonMediaTypeCodec.class, JacksonMediaTypeCodec.class}) // do not expose MapperMediaTypeCodec
@Deprecated(forRemoval = true, since = "4.7")
public class JsonMediaTypeCodec extends JacksonMediaTypeCodec {

    public static final String CONFIGURATION_QUALIFIER = "json";

    /**
     * @param objectMapper To read/write JSON
     * @param applicationConfiguration The common application configurations
     * @param codecConfiguration The configuration for the codec
     */
    public JsonMediaTypeCodec(ObjectMapper objectMapper,
                              ApplicationConfiguration applicationConfiguration,
                              @Named(CONFIGURATION_QUALIFIER) @Nullable CodecConfiguration codecConfiguration) {
        super(objectMapper, applicationConfiguration, codecConfiguration, MediaType.APPLICATION_JSON_TYPE);
    }

    /**
     * @param objectMapper To read/write JSON
     * @param applicationConfiguration The common application configurations
     * @param codecConfiguration The configuration for the codec
     * @deprecated Use {@link #JsonMediaTypeCodec(BeanProvider, CodecConfiguration, ApplicationConfiguration)} instead
     */
    @Deprecated
    public JsonMediaTypeCodec(BeanProvider<ObjectMapper> objectMapper,
                              ApplicationConfiguration applicationConfiguration,
                              @Named(CONFIGURATION_QUALIFIER) @Nullable CodecConfiguration codecConfiguration) {
        super(objectMapper, applicationConfiguration, codecConfiguration, MediaType.APPLICATION_JSON_TYPE);
    }

    /**
     * @param jacksonDatabindMappers To read/write JSON
     * @param applicationConfiguration The common application configurations
     * @param codecConfiguration The configuration for the codec
     */
    @Inject
    public JsonMediaTypeCodec(BeanProvider<JacksonDatabindMapper> jacksonDatabindMappers,
                              @Named(CONFIGURATION_QUALIFIER) @Nullable CodecConfiguration codecConfiguration,
                              ApplicationConfiguration applicationConfiguration) {
        super(jacksonDatabindMappers, applicationConfiguration, MediaType.APPLICATION_JSON_TYPE, codecConfiguration);
    }

    @Override
    public JacksonMediaTypeCodec cloneWithFeatures(JacksonFeatures jacksonFeatures) {
        MapperBuilder<?, ?> builder = getObjectMapper().rebuild();
        jacksonFeatures.getDeserializationFeatures().forEach(builder::configure);
        jacksonFeatures.getSerializationFeatures().forEach(builder::configure);

        return new JsonMediaTypeCodec(builder.build(), applicationConfiguration, codecConfiguration);
    }
}
